<template>
  <div class="header">
    <div class="header-left">
      <h1 class="title">测试数据集生成器</h1>
    </div>
    <div class="header-right">
      <template v-if="!isLoggedIn">
        <el-button 
          type="text" 
          @click="showLoginDialog = true"
          class="login-btn"
        >
          <el-icon><User /></el-icon>
          登录
        </el-button>
      </template>
      <template v-else>
        <el-dropdown @command="handleCommand">
          <span class="user-info">
            <el-icon><User /></el-icon>
            {{ username }}
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </template>
    </div>

    <!-- 登录对话框 -->
    <el-dialog
      v-model="showLoginDialog"
      title="用户登录"
      width="400px"
      :before-close="handleCloseDialog"
    >
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showLoginDialog = false">取消</el-button>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { User, ArrowDown } from '@element-plus/icons-vue'

export default {
  name: 'Header',
  components: {
    User,
    ArrowDown
  },
  setup() {
    const store = useStore()
    const showLoginDialog = ref(false)
    const loginFormRef = ref()
    
    const loginForm = ref({
      username: '',
      password: ''
    })
    
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    const isLoggedIn = computed(() => store.getters.isLoggedIn)
    const username = computed(() => store.getters.username)
    
    const handleLogin = async () => {
      try {
        const valid = await loginFormRef.value.validate()
        if (valid) {
          // 模拟登录
          await store.dispatch('login', {
            username: loginForm.value.username
          })
          ElMessage.success('登录成功')
          showLoginDialog.value = false
          loginForm.value = { username: '', password: '' }
        }
      } catch (error) {
        console.log('登录验证失败', error)
      }
    }
    
    const handleCommand = (command) => {
      if (command === 'logout') {
        store.dispatch('logout')
        ElMessage.success('已退出登录')
      }
    }
    
    const handleCloseDialog = () => {
      showLoginDialog.value = false
      loginForm.value = { username: '', password: '' }
    }
    
    return {
      showLoginDialog,
      loginFormRef,
      loginForm,
      loginRules,
      isLoggedIn,
      username,
      handleLogin,
      handleCommand,
      handleCloseDialog
    }
  }
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  height: 64px;
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  flex-shrink: 0;
  z-index: 100;
}

.header-left {
  flex: 1;
}

.header-right {
  flex-shrink: 0;
}

.title {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.login-btn {
  color: #409eff;
  font-size: 14px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #409eff;
  cursor: pointer;
  font-size: 14px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header {
    padding: 0 16px;
    height: 56px;
  }
  
  .title {
    font-size: 18px;
  }
}

@media (max-width: 480px) {
  .title {
    font-size: 16px;
    max-width: 200px;
  }
}
</style>